﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Goldlight.Mvvm.Base.Infrastructure;
using System.ComponentModel;
using System.Windows.Data;

namespace Goldlight.Mvvm.Base
{
    /// <summary>
    /// The workspace manager controls the collection of items in the workspace.
    /// </summary>
    public static class WorkspaceManager
    {
        #region Members
        private static RangeObservableCollection<WorkspaceViewModelBase> _workspaces;
        #endregion

        /// <summary>
        /// Set the active workspace to a particular item in the workspace collection.
        /// </summary>
        /// <param name="workspace">The workspace to set as active.</param>
        public static void SetActiveWorkspace(WorkspaceViewModelBase workspace)
        {
            ICollectionView icv = CollectionViewSource.GetDefaultView(Workspaces);
            if (icv != null)
            {
                icv.MoveCurrentTo(workspace);
            }
        }

        /// <summary>
        /// Get the workspaces collection.
        /// </summary>
        public static ObservableCollection<WorkspaceViewModelBase> Workspaces
        {
            get
            {
                if (_workspaces == null)
                {
                    _workspaces = new RangeObservableCollection<WorkspaceViewModelBase>();
                    _workspaces.CollectionChanged += OnWorkspacesChanged;
                }
                return _workspaces;
            }
        }

        public static void AddWorkspace(WorkspaceViewModelBase vm)
        {
            Workspaces.Add(vm);
            SetActiveWorkspace(vm);
        }

        /// <summary>
        /// Find the first instance of a <see cref="WorkspaceViewModelBase">workspace</see>.
        /// </summary>
        /// <typeparam name="T">The type of workspace view model.</typeparam>
        /// <returns>The first instance of T if present, null otherwise.</returns>
        /// <remarks>Use this method if you want to create a "singleton" </remarks>
        public static T FindWorkspace<T>() where T : WorkspaceViewModelBase
        {
            T workspace = (T)Workspaces.FirstOrDefault(item => item.GetType() == typeof(T));
            return workspace;
        }

        /// <summary>
        /// Whenever the workspaces collection changes, the mediator updates the workspace
        /// to remove the view model.
        /// </summary>
        private static void OnWorkspacesChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count != 0)
            {
                foreach (WorkspaceViewModelBase workspace in e.NewItems)
                {
                    workspace.Mediator.Register((object o) => {
                        // Add in the ability to close only the appropriate item.
                        if (workspace.InstanceId == ((WorkspaceViewModelBase)o).InstanceId)
                        {
                            _workspaces.Remove(workspace);
                        }
                    }, WorkspaceMessages.RequestCloseWorkspace);
                }
            }
        }
    }
}
